package com.uh.checking.proc;

import com.uh.common.utils.CommandUtils;
import com.uh.checking.config.ConfigException;

import java.io.*;
import java.util.Arrays;
import java.util.jar.Manifest;

import static com.uh.checking.config.JarCheckingConstants.CHARSET;

public class JarCheckingUtils {


    /**
     * definedSteps中从beginStep, endStep之间掐一段
     * @param beginStep
     * @param endStep
     * @param definedSteps
     * @return
     */
    public static String[] trimSteps(String[] definedSteps, String beginStep, String endStep)  {
        String[] steps;
        int beginIdx = 0;
        int endIdx = definedSteps.length - 1;
        for(int idx=0 ; idx < definedSteps.length ; idx ++) {
            String step = definedSteps[idx];
            if(beginStep != null && step.equalsIgnoreCase(beginStep)) {
                beginIdx = idx;
            }
            if(endStep != null && step.equalsIgnoreCase(endStep)) {
                endIdx = idx;
            }
        }

        if(beginIdx > endIdx) {
            throw new ConfigException("Error: beginStep is after endStop. beginStep="
                    + beginStep + ", endStep=" + endStep + ", steps=" + Arrays.toString(definedSteps) );
        }

        int len = (endIdx - beginIdx) + 1;
        steps = new String[len];
        int i = 0;
        for(int idx=beginIdx ; idx<=endIdx ; idx ++) {
            steps[i] = definedSteps[idx];
            i++;
        }

        return steps;
    }


    public static void execute(String cmd) {
        execute(cmd, true, false);
    }

    /**
     * 执行系统命令行
     * @param cmd
     * @param showCmd
     * @param showStdout
     */
    public static void execute(String cmd, boolean showCmd, boolean showStdout) {
        try {
            if(showCmd) {
                System.out.println("$ " + cmd);
            }
            CommandUtils.execute(cmd, CHARSET);
            if(showStdout) {
                System.out.println(CommandUtils.getLastOut());
            }

            String errout = CommandUtils.getLastErr() ;
            if(errout != null && errout.length() > 0) {
                System.out.println(errout);
            }

        }
        catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static void wirteManifest(Manifest mf, File file) {
        try(OutputStream out = new FileOutputStream(file)) {
            mf.write(out);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Manifest readManifext(File manifestFile) {
        Manifest mf = null;
        try(InputStream mfIn = new FileInputStream(manifestFile)) {
            mf = new Manifest(mfIn);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return mf;
    }

    public static String toLocal(String filePath) {
        return filePath.replace('/', File.separatorChar);
    }

    /**
     * 检查表示路径的字符串儿，如果是以 '\' 或 '/' 结尾的就把其删除。
     * @param path
     * @return
     */
    public static String trimEndSeparator(String path) {
        if(path != null && (path.endsWith("\\") || path.endsWith("/"))) {
            return path.substring(0, path.length() - 1);
        }
        else {
            return path;
        }
    }

}
